package school.valon.persistence.dao.impl;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import school.valon.persistence.dao.UserDao;
import school.valon.persistence.dao.base.impl.GenericDaoImpl;
import school.valon.persistence.pojo.User;
import school.valon.persistence.pojo.UserStatus;
import school.valon.persistence.pojo.UserType;

public class UserDaoImpl extends GenericDaoImpl<User, Integer> implements UserDao {

	public UserType findUserTypeById(Integer userTypeId) {
		return em.find(UserType.class, userTypeId);
	}

	public UserStatus findUserStatusById(Integer userStatusId) {
		return em.find(UserStatus.class, userStatusId);
	}
	
	public User findUserByEmail(String userEmail) {
		CriteriaBuilder cr = em.getCriteriaBuilder();
		CriteriaQuery<User> cq = cr.createQuery(User.class);
		Root<User> rootUser = cq.from(User.class);
		cq.where(cr.equal(rootUser.get("email"), userEmail));
		return em.createQuery(cq).getSingleResult();
	}
}
